package com.zly.bscloud.controller;

import com.zly.bscloud.dto.ArticleDTO;
import com.zly.bscloud.service.ArticleService;
import com.zly.bscloud.utils.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article")
@Api(tags = "文章接口")
@CrossOrigin
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping("/hello")
    public String hello(){
        return "hell000999";
    }

    @ApiOperation(value = "新增一个文章")
    @ApiImplicitParam(name = "map" , value = "包含文章的标题,简介,内容" , required = true , dataType = "Map")
    @PostMapping("/save-article")
    public Result saveArticle(@RequestBody Map<String , String> map){
        ArticleDTO articleDTO = articleService.saveArticle(map);
        return Result.createSuccess(articleDTO);
    }

    @ApiOperation(value = "获得全部文章")
    @GetMapping("/get-articles")
    public Result getAllArticles(){
        List<ArticleDTO> articleDTOList = articleService.getAllArticles();
        return Result.createSuccess(articleDTOList);
    }

    @ApiOperation(value = "通过文章id获取一个文章信息")
    @ApiImplicitParam(name = "Id" , value = "文章id" , required = true , dataType = "String")
    @GetMapping("/get-article/{id}")
    public Result getArticleByAId(@PathVariable("id") String Id){
        System.out.println(Id);
        ArticleDTO articleDTO = articleService.getArticleByAId(Id);
        return Result.createSuccess(articleDTO);
    }

    @ApiOperation(value = "通过文章id获取文章的内容")
    @ApiImplicitParam(name = "Id" , value = "文章id" , required = true , dataType = "String")
    @GetMapping("/get-content/{id}")
    public Result getContentByAId(@PathVariable("id") String Id){
        String content = articleService.getContentByAId(Id);
        return Result.createSuccess(content);
    }

    @ApiOperation(value = "通过key获取文章的内容")
    @ApiImplicitParam(name = "key" , value = "关键字" , required = true , dataType = "String")
    @GetMapping("/lucene-search/{key}")
    public Result luceneSearch(@PathVariable("key") String key){
        List<ArticleDTO> articleDTOList = articleService.getArticleByKey(key);
        return Result.createSuccess(articleDTOList);
    }


}
